package org.example.io;

import java.io.File;
import java.io.FileInputStream;

public class TestInputStream {
    public static void main(String[] args) throws Exception{
        // 使用File对象创建流对象
        File file = new File("D:\\tmp\\111.txt");
        FileInputStream stream = new FileInputStream(file);
        // 使用文件名称创建流对象
        FileInputStream inputStream = new FileInputStream("D:\\tmp\\111.txt");

        //读取数据
        //read()返回一个字符,末尾返回-1
        System.out.println((char) stream.read());

        //循环读取数据
        int i=0;
        while ((i=inputStream.read())!=-1){
            System.out.print((char) i);
        }
        System.out.println();

        //利用byte数组读取数据
        byte[] bytes = new byte[1024];
        int len = stream.read(bytes);
        String value = new String(bytes,0,len);
        System.out.println(value);

        //关闭
        inputStream.close();
        stream.close();
    }
}
